package com.ktjy;

import io.minio.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@SpringBootTest
public class MinioTester {
    //本地上传 文件
//    @Test
    public void test() {
        try {
            //根minio服务建立一个连接
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://localhost:9000")
                            .credentials("dvVh7Sn3DFekB3pa5ujn", "NrPOBhkznhcXWLYNolX057BDIx2JuWElEBaZ4Hac")
                            .build();
            //判断桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("zjgd-crm").build());
            if (!found) {//如果桶不存在
                //创建桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("zjgd-crm").build());
            }
            //上传文件
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("zjgd-crm")
                            .object("黑.png")
                            .filename("D:\\dbcj\\media\\picture\\黑.png")
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //上传文件流
    @Test
    public void test2() {
        try {
            //根minio服务建立一个连接
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://localhost:9000")
                            .credentials("dvVh7Sn3DFekB3pa5ujn", "NrPOBhkznhcXWLYNolX057BDIx2JuWElEBaZ4Hac")
                            .build();
            //判断桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("zjgd-crm").build());
            if (!found) {//如果桶不存在
                //创建桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("zjgd-crm").build());
            }
            //上传文件流
            File file = new File("D:\\dbcj\\media\\picture\\云朵与天空.png");
            InputStream in = new FileInputStream(file);

            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket("zjgd-crm")
                            .object("云朵与天空.png")
                            .stream(in, file.length(), -1)
                            .contentType(" image/png")
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
